/**
 * 
 */
package cardbs.dao;

import cardbs.domain.Use;
import static cardbs.servlets.CreateBillServlet.*;
import cardbs.domain.Includes;
import cardbs.domain.Service;
import cardbs.domain.Part;
import cardbs.domain.WorksDoneInService;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


import javax.persistence.EntityManager;
import javax.persistence.Query;

/** 
 * <!-- begin-UML-doc -->
 * <!-- end-UML-doc -->
 * @author marianna
 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class WorksDoneInServiceDAO extends GenericDAO<WorksDoneInService> {
	
	public void save(WorksDoneInService workDoneInService) {
		// begin-user-code
		// TODO Auto-generated method stub
		super.save(workDoneInService);
		// end-user-code
	}
	public Set<WorksDoneInServiceDAO> findUseByService(int serviceCode) {
		// begin-user-code
		// TODO Auto-generated method stub
		EntityManager em = JPAUtil.getCurrentEntityManager();
		Query query = em.createQuery("select use from Use use where serviceCode=:insertedServiceCode");
	    query.setParameter("insertedServiceCode", serviceCode);
	    List <WorksDoneInServiceDAO> results = query.getResultList();
	    Set<WorksDoneInServiceDAO> uses = new HashSet(results);
		int size = results.size();
	    
	    if (size!=0){
	    	return uses;
	    }
		return null;
		// end-user-code
	}
	
	public Boolean findServicesByPartCode(int partcode){
		// begin-user-code
		// TODO Auto-generated method stub
		EntityManager em = JPAUtil.getCurrentEntityManager();
		Query query = em.createQuery("select use from Use use where partCode =:insertedPartCode");
		query.setParameter("insertedPartCode", partcode);
	    List results = query.getResultList();
	    Set services = new HashSet(results);
		int size = results.size();
	   
	    if (size!=0){
	    	return true;
	    }
		return false;
		// end-user-code
	}
	
	
	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param addedUse
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */

	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param service
	 * @param part
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void getPartAmount(Service service, Part part) {
		// begin-user-code
		// TODO Auto-generated method stub

		// end-user-code
	}

	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param service
	 * @return
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public Set<Part> findPartsOfService(Service service) {
		// begin-user-code
		// TODO Auto-generated method stub
		int serviceCode = service.getServiceCode();
		Set<Part> partsOfService = new HashSet<Part>();
		
		EntityManager em = JPAUtil.getCurrentEntityManager();
		Query query = em.createQuery("select use from Use use where serviceCode=:insertedServiceCode");
	    query.setParameter("insertedServiceCode", serviceCode);
	    List <Use> results = query.getResultList();
		
	    int size = results.size();
	    if (size>0){
	     Iterator itr = results.iterator();
	     while(itr.hasNext()){
	    	 Use use = (Use) itr.next();
			 Part part = use.getPart();
			 partsOfService.add(part);
	    	}
			return partsOfService;
	    }
	    return null;
		// end-user-code
	}

	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param service
	 * @param part
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public int findPartAmount(Service service, Part part) {
		// begin-user-code
		// TODO Auto-generated method stub
		// begin-user-code
		// TODO Auto-generated method stub
		int serviceCode = service.getServiceCode();
		int partCode = part.getPartCode();
				
		EntityManager em = JPAUtil.getCurrentEntityManager();
		//EntityManager em = DBUtilities.getEntityManager();
		Query query = em.createQuery("select use from Use use where serviceCode=:insertedServiceCode and partCode=:insertedpartCode");
	    query.setParameter("insertedServiceCode", serviceCode);
	    query.setParameter("insertedpartCode", partCode);
	    List <Use> results = query.getResultList();
		
	    int size = results.size();
	    if (size>0){
	    	Iterator itr = results.iterator();
	    	Use use = (Use)itr.next();
	    	return use.getAmount(); 
	    }
	    return 0;
		// end-user-code
	}

	
}